package com.yanqu.road.server.cmd.assistant;

import com.yanqu.road.entity.assistant.enums.eAssistantEscortCartState;
import com.yanqu.road.entity.beautyescort.EscortBeautyData;
import com.yanqu.road.entity.beautyescort.EscortBeautyDispatch;
import com.yanqu.road.entity.beautyescort.EscortBeautyEvent;
import com.yanqu.road.entity.beautyescort.EscortDartTypeInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.log.LogBeautyEscorting;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.assistant.AssistantProto;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.assistant.handler.UnionCrossActionHandler;
import com.yanqu.road.server.manger.assistant.handler.ZsCrossActionHandler;
import com.yanqu.road.server.manger.beautyescort.BeautyEscortMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.stream.Collectors;

@Cmd(code = GameProtocol.S_ASSISTANT_ESCORT_ACTION_FROM_CROSS, desc = "押镖返回")
public class AssistantEscortActionFromCross extends AbstractCommand {

    private static Logger logger = LogManager.getLogger(AssistantEscortActionFromCross.class.getName());

    private AssistantProto.AssistantCrossActionRespMsg.Builder builder;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        builder = AssistantProto.AssistantCrossActionRespMsg.parseFrom(packet.getBytes()).toBuilder();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 处理押镖响应
        AssistantProto.AssistantActionReqMsg.Builder assistantReq = builder.getAssistantActionReqMsg().toBuilder();
        AssistantProto.AssistantActionRespMsg.Builder assistantResp = builder.getAssistantActionRespMsg().toBuilder();
        for (BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp noDispatch : builder.getNoDispatchListList()) {
            EscortBeautyData beautyData = player.getModule(BeautyEscortModule.class).getByBeautyId(noDispatch.getBeautyId());
            beautyData.setEscortTimes(beautyData.getEscortTimes() - 1);
        }
        // 免费镖车，type 0
        EscortDartTypeInfo typeInfo = ConfigMgr.getDartTypeInfoMap().get(0);
        for (AssistantProto.AssistantBeautyEscortCartTemp cartTemp : assistantResp.getAssistantBeautyEscortCartRespMsg().getCartListList()) {
            BeautyEscortProto.BeautyEscortCartSimpleTemp cartInfo = cartTemp.getCart();
            // 判断是否进行押镖
            if (cartTemp.getDispatchType() == eAssistantEscortCartState.Dispatch.getIntValue() || cartTemp.getDispatchType() == eAssistantEscortCartState.FinishAndDispatch.getIntValue()) {
                List<EscortBeautyDispatch> dispatchList = BeautyEscortMgr.parseBeautyDispatch(cartTemp.getDispatchBeautyList());
                // 押镖日志
                LogBeautyEscorting log;
                // 商会等级限制
                UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
                if (unionInfo == null) {
                    AutoLogMgr.add(new LogBeautyEscorting(player.getUserId(), cartInfo.getCartId(), cartInfo.getDartId(), "", 0, dispatchList, typeInfo.getConsume(),
                            "", cartInfo.getClientId(), cartInfo.getTypeId()));
                } else {
                    int unionLv = UnionConfigMgr.getUnionLv(unionInfo.getExperience());
                    AutoLogMgr.add(new LogBeautyEscorting(player.getUserId(), cartInfo.getCartId(), cartInfo.getDartId(), player.getUserInfo().getUnionUid(), unionLv, dispatchList, typeInfo.getConsume(),
                            "", cartInfo.getClientId(), cartInfo.getTypeId()));
                }
                // 押镖任务
                player.notifyListener(eGamePlayerEventType.BeautyEscortTimes.getValue(), 1);
            }
            // 处理事件奖励
            BeautyEscortProto.BeautyEscortGetEventRewardRespMsg eventReward = cartTemp.getEventReward();
            if (eventReward.hasReward()) {
                List<EscortBeautyDispatch> dispatchList = BeautyEscortMgr.parseBeautyDispatch(eventReward.getBeautyList());
                EscortBeautyEvent beautyEvent = new EscortBeautyEvent();
                beautyEvent.setUnionUid(eventReward.getUnionUid());
                beautyEvent.setUnionLv(eventReward.getUnionLv());
                beautyEvent.setEventTime(eventReward.getEventTime());//事件生成时间点
                beautyEvent.setDamage(eventReward.getAllDamage());
                beautyEvent.setEventId(eventReward.getEventId());
                player.getModule(BeautyEscortModule.class).addEventReward(eventReward.getReward(), eventReward.getExp(), dispatchList, eventReward.getDartId(), beautyEvent);
            }
            // 处理镖车奖励
            BeautyEscortProto.BeautyEscortGetDartRewardRespMsg dartReward = cartTemp.getDartReward();
            if (dartReward.hasReward()) {
                List<EscortBeautyDispatch> dispatchList = BeautyEscortMgr.parseBeautyDispatch(dartReward.getBeautyList());
                player.getModule(BeautyEscortModule.class).addDartReward(dartReward.getReward(), dartReward.getExp(), dispatchList, dartReward.getDartId());
            }
        }
        // 添加派遣日志
        String dispatchIds = builder.getNoDispatchListList().stream().map(beautyDispatch -> String.valueOf(beautyDispatch.getBeautyId())).collect(Collectors.joining(","));
        logger.info("小玉助手自动押镖-跨服返回,玩家: {}, 未派遣美女Id列表: {}", player.getUserId(), dispatchIds);
        builder.setAssistantActionRespMsg(assistantResp);
        //走镖红点
        BeautyEscortProto.CrossBeautyEscortRedDotReqMsg.Builder reqMsg = BeautyEscortProto.CrossBeautyEscortRedDotReqMsg.newBuilder();
        player.sendPacket(Protocol.C_BEAUTY_ESCORT_RED_DOT_SYNC, reqMsg);
        // 招商操作
        new ZsCrossActionHandler().handle(player, assistantReq, assistantResp);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}

